var ctx = $("#ctx").val();
$(function () {
    var loginDialog = new mdui.Dialog('.mc-login');
    var registerDialog = new mdui.Dialog('.mc-register');
    $(".mc-login-btn").click(function () {
        var localUsername = localStorage.getItem("username");
        var localPassword = localStorage.getItem("password");
        if (localUsername != null && localPassword != null) {
            $("#loginForm input[name=username]").val(localUsername);
            $("#loginForm input[name=password]").val(localPassword);
            $("#loginForm #rememberMe").attr("checked", "checked");
            $("#loginForm .mdui-textfield").eq(0).addClass("mdui-textfield-not-empty");
            $("#loginForm .mdui-textfield").eq(1).addClass("mdui-textfield-not-empty");
        }
        loginDialog.open();
    });
    $(".mc-register-btn").click(function () {
        $(".captcha").trigger("click");
        registerDialog.open();
    });
    $(".mc-login .close").click(function () {
        loginDialog.close();
    });
    $(".mc-register .close").click(function () {
        registerDialog.close();
    });
    $(".mc-register-trigger").click(function () {
        loginDialog.close();
        $(".captcha").trigger("click");
        registerDialog.open();
    });

    //登录
    $(".mc-login").keyup(function (event) {
        if (event.keyCode == 13) {
            $(".login-button").trigger("click");
        }
    });
    $(".login-button").click(function () {
        var inputs = $("#loginForm input");
        var login = true;
        $.each(inputs, function (index, input) {
            if ($(input).val() == "") {
                login = false;
            }
        });
        if (login == false) {
            mdui.snackbar({
                message: "各项不为空",
                position: "top"
            });
        } else {
            if ($("#rememberMe").is(":checked")) {
                localStorage.setItem("username", $("#loginForm input[name=username]").val());
                localStorage.setItem("password", $("#loginForm input[name=password]").val());
            } else {
                localStorage.removeItem("username");
                localStorage.removeItem("password");
            }
            $.ajax({
                url: ctx + "/user/login",
                type: "POST",
                data: $("#loginForm").serialize(),
                success: function (data) {
                    if (data.code == 0) {
                        mdui.snackbar({
                            message: "登陆成功",
                            position: "top"
                        });
                        setTimeout(function () {
                            location.reload();
                        }, 1000)
                    } else {
                        mdui.snackbar({
                            message: data.msg,
                            position: "top"
                        });
                    }
                }
            });
        }
    });

    //注册
    $(".register-button").click(function () {
        var inputs = $("#registerForum input");
        var login = true;
        inputs.remove
        for (var i = 1; i < inputs.length; i++) {
            if (inputs.eq(i).val() == "") {
                login = false;
            }
        }
        if (login == false) {
            mdui.snackbar({
                message: "各项不为空",
                position: "top"
            });
        } else if ($("#registerForum input[name=password]").val() != $("#registerForum input[name=password2]").val()) {
            mdui.snackbar({
                message: "两次密码不一致",
                position: "top"
            });
        } else {
            $.ajax({
                url: ctx + "/user/register",
                type: "POST",
                data: $("#registerForum").serialize(),
                success: function (data) {
                    if (data.code == 0) {
                        mdui.snackbar({
                            message: "注册成功！",
                            position: "top"
                        });
                        registerDialog.close();
                        loginDialog.open();
                    } else {
                        mdui.snackbar({
                            message: data.msg,
                            position: "top"
                        });

                    }
                }
            });
        }
        $(".captcha").trigger("click");
    });
    $(".captcha").click(function () {
        $(this).prop("src", ctx + "/captcha?date=" + new Date());
    });


    //注销
    $(".bottom button").click(function () {
        $.ajax({
            url: ctx + "/user/logout",
            type: "GET",
            success: function (data) {
                if (data.code == 0) {
                    mdui.snackbar({
                        message: "注销成功",
                        position: "top"
                    });
                    setTimeout(function () {
                        location.href = ctx + "/";
                    }, 1000);
                } else {
                    mdui.snackbar({
                        message: data.msg,
                        position: "top"
                    });
                }
            }
        });
    });
    //上传
    $(".upload-btn").click(function () {
        $("input[type='file']").trigger("click");
    });
    $("input[type='file']").change(function () {
        var formData = new FormData($(this));
        $.ajaxFileUpload({
            type: "post",
            url: ctx + "/user/uploadavatar",
            secureuri: false,
            fileElementId: 'avatar',
            dataType: "text",
            success: function (data) {
                //ajaxfileupload使用iframe+form的方式实现异步上传文件 无法直接解析body
                var json = $.parseJSON($(data).html());
                if (json.code == 0) {
                    location.reload();
                } else {
                    mdui.snackbar({
                        message: "上传失败",
                        position: "top"
                    });
                }
            }
        });
    });

    //返回顶部
    $("#scrolltop-btn").click(function(e) {
        $('body,html').animate({
            scrollTop: 0
        }, 600)
    });
    scrolltop();
});

//返回顶部
function scrolltop() {
    $(window).scroll(function(e) {
        if ($(window).scrollTop() > 80 && $(document).height() != $(window).height() + $(window).scrollTop())
            document.getElementById("scrolltop-btn").classList.remove("mdui-fab-hide");
        else
            document.getElementById("scrolltop-btn").classList.add("mdui-fab-hide")
    })
}